Reference: CCP_L01_C01_Simple Calculator
Title: Build a Simple Calculator in C with
History Feature – Perfect for Beginners!
Introduction
Are you a student or beginner learning C programming? A Simple Calculator in C is an excellent academic project that helps
you understand basic programming
concepts, file handling, and modular design. In this blog, we will walk
through how to develop a calculator in C
that performs basic arithmetic operations and stores the calculation history
for future reference. 🚀
📌 Project
Overview
This Simple
Calculator
allows users to:
 ✅ Perform basic arithmetic operations (+, -, *, /)
 ✅ Handle division by zero errors
 ✅ Save calculations to a history file
 ✅ View past calculations
 ✅ Clear stored history
It follows best coding practices and adheres to modular programming principles, making it easy to understand and modify.
📌 Features &
Functionality
💡 1. Perform Calculations
 Users can input an expression like 5 + 3,
and the program computes and displays the result.
💡 2. View Calculation History
 The program reads from a file (calculation_history.txt) and displays all past calculations.
💡 3. Clear History
 A feature to delete stored calculations and start fresh.
💡 4. Exit Option
 The user can exit anytime.
📌 Code
Implementation
The program follows a menu-driven approach to guide users
through different options. Below is the C
source code for the project:
#include <stdio.h>
#include <stdlib.h>
void displayMenu();
float calculate(float num1, float num2, char
operator);
void saveHistory(float num1, float num2, char
operator, float result);
void viewHistory();
void clearHistory();
#define HISTORY_FILE
"calculation_history.txt"
int main() {
   
float num1, num2, result;
    char
operator, choice;
    
   
while (1) {
       
displayMenu();
       
printf("Enter your choice: ");
       
scanf(" %c", &choice);
        
       
switch (choice) {
           
case '1':  
               
printf("Enter an expression (e.g., 5 + 3): ");
               
if (scanf("%f %c %f", &num1, &operator, &num2) !=
3) {
                    printf("Invalid input!
Try again.\n");
                    while(getchar() != '\n'); 
                    continue;
               
}
               
result = calculate(num1, num2, operator);
               
printf("Result: %.2f\n", result);
               
saveHistory(num1, num2, operator, result);
               
break;
           
           
case '2':  
               
viewHistory();
               
break;
               
           
case '3':  
               
clearHistory();
               
break;
               
           
case '4':  
               
printf("Exiting calculator...\n");
               
return 0;
               
           
default:
               
printf("Invalid choice! Please enter a number between
1-4.\n");
       
}
    }
}
void displayMenu() {
   
printf("\n==== Simple Calculator ====\n");
   
printf("1. Perform Calculation\n");
   
printf("2. View Calculation History\n");
   
printf("3. Clear Calculation History\n");
   
printf("4. Exit\n");
   
printf("===========================\n");
}
float calculate(float num1, float num2, char
operator) {
   
switch (operator) {
       
case '+': return num1 + num2;
       
case '-': return num1 - num2;
       
case '*': return num1 * num2;
       
case '/':
           
if (num2 == 0) {
               
printf("Error: Division by zero is not allowed.\n");
               
return 0;
           
}
           
return num1 / num2;
       
default:
           
printf("Error: Invalid operator.\n");
           
return 0;
    }
}
void saveHistory(float num1, float num2, char
operator, float result) {
    FILE
*file = fopen(HISTORY_FILE, "a");
    if
(file == NULL) {
       
printf("Error: Could not open history file.\n");
       
return;
    }
   
fprintf(file, "%.2f %c %.2f = %.2f\n", num1, operator, num2,
result);
   
fclose(file);
}
void viewHistory() {
    char
line[50];
    FILE
*file = fopen(HISTORY_FILE, "r");
    if
(file == NULL) {
       
printf("No calculation history found.\n");
       
return;
    }
   
printf("\n==== Calculation History ====\n");
   
while (fgets(line, sizeof(line), file)) {
       
printf("%s", line);
    }
   
fclose(file);
}
void clearHistory() {
    FILE
*file = fopen(HISTORY_FILE, "w");
    if
(file != NULL) {
       
fclose(file);
       
printf("Calculation history cleared.\n");
    }
else {
       
printf("Error: Could not clear history.\n");
    }
}
📌 How to Run the
Code in Code::Blocks
1️⃣ Install Code::Blocks with MinGW Compiler
 2️⃣ Create a new "C Console
Application" project
 3️⃣ Paste the above C code
 4️⃣ Compile & Run (Press F9)
📌 Expected Output
==== Simple Calculator ====
1. Perform Calculation
2. View Calculation History
3. Clear Calculation History
4. Exit
===========================
Enter your choice: 
Users can enter a choice and interact
with the calculator.
📌 Why This
Project is Great for Beginners
✅
Teaches Basic C
Concepts
– Variables, loops, functions, file handling
 ✅ Hands-on Practice – Learn error handling and user input validation
 ✅ Expandable – Can be enhanced with scientific functions or a GUI
📌 Conclusion
The Simple
Calculator in C is a great beginner-friendly project. It introduces core programming concepts, enhances logical thinking, and is a great academic project. Try it out and
build your own advanced version! 🚀
📌 Download Full Code & More C Projects
Below In the eBook link! 👇
------------------------ 
Brief About “C Code Projects for Beginner Students (Ages
8-16)" eBook
Are you a school student aged 8 to 16
with a budding interest in programming, or perhaps looking for a hands-on way
to master C language for your academic projects? Look no further! I am thrilled
to announce the launch of "C Code Projects for Beginner Students,"
your ultimate guide to practical C programming.
Ready to start your coding adventure?
[Click below any links to get your copy of
"C Code Projects for Beginner Students (Ages 8-16)"!] 
eBook CCP_L01 Link: 
https://play.google.com/store/books/details?id=KS54EQAAQBAJ  [Google Play Store]
https://books.google.co.in/books?id=KS54EQAAQBAJ   [Google Books]
Enjoy this eBook CCP_L01 on ‘C Code Projects
for Beginner Students’ series and do not forget to explore other resources
related to this series eBook. Thanks for visiting my blog. 
EBOOK CCP_L01 promotion Blog Link: 
https://myspacemywork2024.blogspot.com/2025/08/unlock-world-of-code-introducing-c-code.html
Happy Reading!
…till next
post, bye-bye & take care!
No comments:
Post a Comment